/* **************************************************************
NAME: Main_YR.do 
CREATED: 18 November 2020 
EDITED: 13 December 2021 
AUTHOR: callan.corcoran@kellogg.northwestern.edu
PURPOSE: To create a replication dataset / tables for Yemen R&R 
************************************************************** */

// **************************************************************
// INITIALIZE STATA 

clear all
mat drop _all
set more off
cap log close
set maxvar 20000
set type double 

* pause on // Un-comment this if you would like to be prompted to review statistics 

// Install programs 
cap ssc install esttab 
cap ssc install estout 
cap ssc install svmat 
cap ssc install leebounds 

gl user = c(username)

if "$user"=="lfb5535" { // Lasse
	gl main "C:\Users\lfb5335\Dropbox\Research\14_PostDoubleYemen\TUP Yemen Data" 
}

if "$user"=="ccc5481" { // Add user-specific path to the folder above the Replication Folder 
	gl main "C:/Users/ccc5481/Dropbox/TUP Yemen Data" 
}

else {
	gl main "C:/Users/$user/Dropbox/TUP Yemen Data"
}

gl replication "$main/Yemen_Replication"

qui do "$replication/Globals_YR.do"


// **************************************************************
// IMPORT DATA 

use "${rep_analysis}.dta", clear 

// **************************************************************
// CREATE MAIN SUBMISSION (PAPER) TABLES 
// 	For more detail on the below programs and options, see Programs folder  

// TABLE 1-1 (BALANCE)
	yemenbalancetable num_adults_bsl nb_children_bsl hhsize_bsl avg_age_bsl	avg_edu_years_bsl age_head_bsl hh_head_over60_bsl gender_head_bsl hh_head_educ_bsl disabl_head_bsl ///
		if endline_survey==1, ///
		filename("Table1") title("Table 1: Balance Test of Treatment Assignment for Endline Households Only") /// 
		winsorize foldername("$rep_output") 
	
{ // TABLE 1-2 (F-TESTS)
	loc balancevar treatment 
	
	loc ifif "if endline_survey==1"

	loc ballist1 num_adults_win1_bsl nb_children_win1_bsl hhsize_win1_bsl avg_age_win1_bsl	avg_edu_years_win1_bsl gender_head_win1_bsl age_head_win1_bsl hh_head_over60_win1_bsl hh_head_educ_win1_bsl disabl_head_win1_bsl // Table 1 

	loc ballist2 asset_index_win1_bsl ctotalpc_win1_bsl ctotalhh_win1_bsl inc_total_win1_bsl ls_hany_win1_bsl inonagriself_win1_bsl fs_index_win1_bsl savings_win1_bsl percep_econ_win1_bsl hous_sindex_win1_bsl debt_index_win1_bsl // Table 2 (proxies included) // 

	loc ballist3 anyshock_win1_bsl dshock_lstock_win1_bsl shockresp_savings_win1_bsl shockresp_loan_win1_bsl shockresp_asset_win1_bsl hhsize_win1_bsl avg_edu_years_win1_bsl avg_age_win1_bsl hh_head_died_win1_bsl num_died_win1_bsl num_died_ill_win1_bsl num_died_acdnt_win1_bsl avgage_died_win1_bsl m_avgage_died_win1_bsl // Table 3 (proxies omitted)
	
	loc balall `ballist1' `ballist2' `ballist3'

	preserve 
	
	replace avgage_died_win1_bsl = 0 if missing(avgage_died_win1_bsl)
	
	foreach set in list1 list2 list3 all {
		qui reg `balancevar' `bal`set'' i.village `ifif', r 
		test `bal`set''
	}

	restore
}

pause 
	
// TABLE 2-1 (KEY WELFARE OUTCOMES)
	yemenstandardtable asset_tot_value_end ctotalpc_end ctotalhh_end inc_total_end inc_LS_end inc_nonLS_end fs_index_end savings_index_end percep_econ_end hous_sindex_end debt_index_end /// 
		if endline_survey==1, ///
		filename("Table2") title("Table 2: Treatment Effects on Key Welfare Outcomes") /// 
		winsorize baselinevals foldername("$rep_output") /// 
		balance(treatment)
	
// TABLE 2-2 (MULTIPLE HYPOTHESIS CORRECTION)
	yemenmhctable asset_tot_value_end ctotalpc_end ctotalhh_end inc_total_end inc_LS_end inc_nonLS_end fs_index_end savings_index_end percep_econ_end hous_sindex_end debt_index_end /// 
		if endline_survey==1, ///
		filename("Table2_MHC") title("Table 2: Treatment Effects on Key Welfare Outcomes with Multiple Hypothesis Correction") /// 
		winsorize baselinevals foldername("$rep_output") 
		
{ // TABLE 3 (SECONDARY OUTCOMES)
		
// TABLE 3A (SHOCKS)
	yemenstandardtable anyshock_end dshock_lstock_end shockresp_savings_end shockresp_loan_end shockresp_asset_end /// 
		if endline_survey==1, ///
		filename("Table3A") title("Table 3A: Treatment Effects on Shocks in Past 12m") /// 
		winsorize baselinevals foldername("$rep_output") balance(treatment) 
		
// TABLE 3B (HOUSEHOLD CONSUMPTION)
	yemenstandardtable hhsize_end avg_edu_years_end avg_age_end new_perm_mbs_end num_new_hh_mbs_end hh_head_new_mb_end ///		
		if endline_survey==1, ///
		filename("Table3B") title("Table 3B: Treatment Effects on Household Composition") ///
		winsorize baselinevals foldername("$rep_output") /// 
		balance(treatment) 
		
// TABLE 3C (MORTALITY)
	yemenstandardtable hh_head_died_end num_died_end num_old_died_end num_died_ill_end num_died_acdnt_end avgage_died_end /// 	
		if endline_survey==1, ///
		filename("Table3C") title("Table 3C: Treatment Effects on Mortality") ///
		winsorize baselinevals foldername("$rep_output") /// 
		balance(treatment) 
		
// TABLE 3D (TRAVEL)
	yemenstandardtable num_travel_30d_end prop_travel_30d_end num_wrktravel_30d_end prop_wrktravel_30d_end ///
		if endline_survey==1, ///
		filename("Table3D") title("Table 3D: Treatment Effects on Travel") ///
		winsorize baselinevals foldername("$rep_output") /// 
		balance(treatment) 
		
// TABLE 3E (TRANSFERS)
	yemenstandardtable gift_total_end totalremit_end ///
		if endline_survey==1, ///
		filename("Table3E") title("Table 3E: Treatment Effects on Inter-Household Transfers") ///
		winsorize baselinevals foldername("$rep_output") balance(treatment) 
}
	
// ATABLE 1 (EXCLUSION ANALYSIS)
	yemenbalancetable num_inc_sources_bsl hhsize_bsl nb_children_bsl nb_room_bsl inonagriself_bsl d3_0_bsl g2_0_bsl loanask_bsl loanformal_bsl loaninformal_bsl g3_3_bsl d2_0_bsl asset_have1_bsl asset_have2_bsl asset_have5_bsl asset_have3_bsl asset_have4_bsl asset_have11_bsl asset_have18_bsl c6_5_2_bsl /// 
	if treatment==1, /// Replacing swf_income_bsl with inonagriself_bsl
	filename("ATable1") title("Appendix Table 1: Balance Test of Eligibility Among Households Assigned to Treatment") /// 
	balancevar(eligible) foldername("$rep_output")
	
// ATABLE 2 (ATTRITION)
	yemenattritiontable num_adults_bsl nb_children_bsl hhsize_bsl avg_age_bsl avg_edu_years_bsl gender_head_bsl age_head_bsl hh_head_over60_bsl hh_head_educ_bsl disabl_head_bsl /// Table 1 variables 
		asset_index_bsl ctotalpc_bsl ctotalhh_bsl inc_total_bsl ls_hany_bsl inonagriself_bsl fs_index_bsl savings_bsl percep_econ_bsl hous_sindex_bsl debt_index_bsl, /// Table 2 variables // Replacing swf_income_bsl with inonagriself_bsl
		filename("ATable2") title("Appendix Table 2: Endline Survey Response Rate Analysis") /// 
		winsorize foldername("$rep_output") ///
		scale ftest
		
pause 
		
// ATABLE 3 (BALANCE)
	loc balappend asset_index_bsl ctotalpc_bsl ctotalhh_bsl inc_total_bsl ls_hany_bsl inonagriself_bsl fs_index_bsl savings_bsl percep_econ_bsl hous_sindex_bsl debt_index_bsl /// Table 2 (proxies included) // Replacing swf_income_bsl with inonagriself_bsl
	anyshock_bsl dshock_lstock_bsl shockresp_savings_bsl shockresp_loan_bsl shockresp_asset_bsl hhsize_bsl avg_edu_years_bsl avg_age_bsl hh_head_died_bsl num_died_bsl num_died_ill_bsl num_died_acdnt_bsl avgage_died_bsl // Table 3 (proxies omitted)

	yemenbalancetable `balappend' ///
		if endline_survey==1, ///
		filename("ATable3") title("Appendix Table 3: Balance Test of Treatment Assignment") /// 
		winsorize foldername("$rep_output")
	
// ATABLE 4 (ASSETS)
	yemenstandardtable asset_tot_value_end asset_hh_value_end asset_prod_value_end asset_prodNonLS_end lstock_value_end asset_goatsheep_end, ///
		filename("ATable4") title("Appendix Table 4: Treatment Effects on Asset Values") ///
		winsorize baselinevals foldername("$rep_output")
	
// ATABLE 5A (PER-CAPITA CONSUMPTION)
	yemenstandardtable ctotalpc_end cfood_adjpcmonth_end cnonfood_adjpcmonth_end cdurable_adjpcmonth_end ceduc_adjpcmonth_end chealth_adjpcmonth_end ctotal_adjNTmonth_end, ///
		filename("ATable5A") title("Appendix Table 5A: Treatment Effects on Monthly Consumption per Capita") ///
		winsorize baselinevals foldername("$rep_output")
	
// ATABLE 5B (HOUSEHOLD CONSUMPTION)
	yemenstandardtable ctotalhh_end cfood_adjhhmonth_end cnonfood_adjhhmonth_end cdurable_adjhhmonth_end ceduc_adjhhmonth_end chealth_adjhhmonth_end, ///
		filename("ATable5B") title("Appendix Table 5B: Treatment Effects on Monthly Consumption Total per Household") ///
		winsorize baselinevals foldername("$rep_output")
	
// ATABLE 6 (LIVESTOCK INCOME)
	yemenstandardtable inc_LS_end ranimals_annual_end lstock_costs_end lstockin_total_end LB7_1_end LB7_2_end LB7_3_end LB7_4_end LB7_oth_end lv_asset_spent_end, ///
		filename("ATable6") title("Appendix Table 6: Treatment Effects on Livestock Income in Past 12m") ///
		winsorize baselinevals foldername("$rep_output")
	
// ATABLE 7 (NON-LIVESTOCK INCOME)
	yemenstandardtable inc_nonLS_end ipaidlabor_annual_end ibusiness_annual_end rbusiness_annual_end totalbuscosts_yr_end iagri_annual_end ragri_annual_end totalagricost_end, ///
		filename("ATable7") title("Appendix Table 7: Treatment Effects on Non-Livestock Income in Past 12m") ///
		winsorize baselinevals foldername("$rep_output")
	
// ATABLE 8 (FOOD SECURITY)
	yemenstandardtable fs_index_end fs_enoughfood_end fs_twomeals_end fs_adultskip_end fs_wholeday_end fs_childskip_end, ///
		filename("ATable8") title("Appendix Table 8: Treatment Effects on Food Security") ///
		winsorize baselinevals foldername("$rep_output")
	
// ATABLE 9 (SAVINGS)
	yemenstandardtable savings_index_end sav_totalamt_end sav_depositamt_end, ///
	filename("ATable9") title("Appendix Table 9: Treatment Effects on Savings") ///
	winsorize baselinevals foldername("$rep_output")
	
// ATABLE 10 (HOUSING)
	yemenstandardtable hous_sindex_end nb_rooms_apc_end prim_roof_concrete_end prim_walls_stone_end latrine_end dohhrepair_end hhrepair_spend_end hhrepair_plan_end treated_water_end inv_time_water_end, ///
	filename("ATable10") title("Appendix Table 10: Treatment Effects on Housing") ///
	winsorize baselinevals foldername("$rep_output")
	
// ATABLE 11 (LENDING)
	yemenstandardtable loan_informalamt_end loan_formalamt_end amt_debt_end, ///
		filename("ATable11") title("Appendix Table 11: Treatment Effects on Lending and Debts") ///
		winsorize baselinevals foldername("$rep_output")
	
use "${rep_analysis}.dta", clear 
	
// ATABLE 12 (BOUNDS)
	yemenboundstable asset_tot_value_end ctotalpc_end ctotalhh_end inc_total_end inc_LS_end inc_nonLS_end fs_index_end savings_index_end percep_econ_end hous_sindex_end debt_index_end /// 
		if endline_survey==1, ///
		filename("ATable12") title("Appendix Table 12: Adjusted Bounds for Treatment Effects on Key Welfare Outcomes") /// 
		winsorize baselinevals foldername("$rep_output")
	
{ // ATABLE 13 (INVERSE PROBABILITY WEIGHTING OF KEY OUTCOMES)
	// Set controls 
	loc ipwcontrols num_adults_bsl nb_children_bsl hhsize_bsl avg_age_bsl	avg_edu_years_bsl gender_head_bsl age_head_bsl hh_head_over60_bsl hh_head_educ_bsl disabl_head_bsl
	
	// Loop through "simple" and "fancy" IPW
	foreach type in simple fancy {
		// Set label 
		loc typelabel = substr(proper("`type'"), 1, 4) 

		yemenstandardtable asset_tot_value_end ctotalpc_end ctotalhh_end inc_total_end inc_LS_end inc_nonLS_end fs_index_end savings_index_end percep_econ_end hous_sindex_end debt_index_end, ///
			filename("ATable13_`typelabel'IPW") title("Appendix Table 13: Treatment Effects on Key Welfare Outcomes with Inverse Probability Weighting") /// 
			baselinevals winsorize `type'ipw /// 
			addlcontrols(`ipwcontrols') foldername("$rep_output")
	}	
	
	// Run IPW with bootstrapping 
	// This takes a long time, so run this only once / run separately 
/*
	qui yemenreweightingtable asset_tot_value_end ctotalpc_end ctotalhh_end inc_total_end inc_LS_end inc_nonLS_end fs_index_end savings_index_end percep_econ_end hous_sindex_end debt_index_end, /// 
		numboots(5000) /// 
		filename("ATable13_BootstrapIPW") title("Appendix Table 13: Treatment Effects on Key Welfare Outcomes with Bootstrapping Inverse Probability Weighting") /// 
		baselinevals winsorize /// 
		addlcontrols(`ipwcontrols') foldername("$rep_output")
*/
}

{ // ATABLE 14 (INVERSE PROBABILITY WEIGHTING OF ADDITIONAL OUTCOMES)
	// Set controls 
	loc ipwcontrols num_adults_bsl nb_children_bsl hhsize_bsl avg_age_bsl avg_edu_years_bsl gender_head_bsl age_head_bsl hh_head_over60_bsl hh_head_educ_bsl disabl_head_bsl
	
	// Loop through "simple" and "fancy" IPW
	foreach type in simple fancy {
		// Set label 
		loc typelabel = substr(proper("`type'"), 1, 4) 

		// ATABLE 14A (SHOCKS)
		yemenstandardtable anyshock_end dshock_lstock_end shockresp_savings_end shockresp_loan_end shockresp_asset_end, /// 
			filename("ATable14A_`typelabel'IPW") title("Appendix Table 14A: Treatment Effects on Shocks in Past 12m with Inverse Probability Weighting") /// 
			winsorize baselinevals `type'ipw /// 
			addlcontrols(`ipwcontrols') foldername("$rep_output")
			
		// ATABLE 14B (HOUSEHOLD CONSUMPTION)
		yemenstandardtable hhsize_end avg_edu_years_end avg_age_end new_perm_mbs_end num_new_hh_mbs_end hh_head_new_mb_end, ///
			filename("ATable14B_`typelabel'IPW") title("Appendix Table 14B: Treatment Effects on Household Composition with Inverse Probability Weighting") ///
			winsorize baselinevals `type'ipw /// 
			addlcontrols(`ipwcontrols') foldername("$rep_output")
			
		// ATABLE 14C (MORTALITY)
		yemenstandardtable hh_head_died_end num_died_end num_old_died_end num_died_ill_end num_died_acdnt_end avgage_died_end, ///
			filename("ATable14C_`typelabel'IPW") title("Appendix Table 14C: Treatment Effects on Mortality with Inverse Probability Weighting") ///
			winsorize baselinevals `type'ipw /// 
			addlcontrols(`ipwcontrols') foldername("$rep_output")
			
		// ATABLE 14D (TRAVEL)
		yemenstandardtable num_travel_30d_end prop_travel_30d_end num_wrktravel_30d_end prop_wrktravel_30d_end, ///
			filename("ATable14D_`typelabel'IPW") title("Appendix Table 14D: Treatment Effects on Travel with Inverse Probability Weighting") ///
			winsorize baselinevals `type'ipw /// 
			addlcontrols(`ipwcontrols') foldername("$rep_output")
	
		// ATABLE 14E (TRANSFERS)
		yemenstandardtable gift_total_end totalremit_end, ///
			filename("ATable14E_`typelabel'IPW") title("Appendix Table 14E: Treatment Effects on Inter-Household Transfers with Inverse Probability Weighting") ///
			winsorize baselinevals `type'ipw /// 
			addlcontrols(`ipwcontrols') foldername("$rep_output")
	}	
	
	// Run IPW with bootstrapping 
	// This takes a long time, so run this only once / run separately 
	/*	// ATABLE 14A (SHOCKS)
		qui yemenreweightingtable anyshock_end dshock_lstock_end shockresp_savings_end shockresp_loan_end shockresp_asset_end, /// 
			filename("ATable14A_BootstrapIPW") title("Appendix Table 14A: Treatment Effects on Shocks in Past 12m with Bootstrapping Inverse Probability Weighting") /// 
			numboots(5000) /// 
			baselinevals winsorize /// 
			addlcontrols(`ipwcontrols') foldername("$rep_output")
	
		// ATABLE 14B (HOUSEHOLD CONSUMPTION)
		yemenreweightingtable hhsize_end avg_edu_years_end avg_age_end new_perm_mbs_end num_new_hh_mbs_end hh_head_new_mb_end, ///
			filename("ATable14B_BootstrapIPW") title("Appendix Table 14B: Treatment Effects on Household Composition with Bootstrapping Inverse Probability Weighting") ///
			numboots(5000) /// 
			baselinevals winsorize /// 
			addlcontrols(`ipwcontrols') foldername("$rep_output")
			
		// ATABLE 14C (MORTALITY)
		yemenreweightingtable hh_head_died_end num_died_end num_old_died_end num_died_ill_end num_died_acdnt_end avgage_died_end, ///
			filename("ATable14C_BootstrapIPW") title("Appendix Table 14C: Treatment Effects on Mortality with Bootstrapping Inverse Probability Weighting") ///
			numboots(5000) /// 
			baselinevals winsorize /// 
			addlcontrols(`ipwcontrols') foldername("$rep_output")
			
		// ATABLE 14D (TRAVEL)
		yemenreweightingtable num_travel_30d_end prop_travel_30d_end num_wrktravel_30d_end prop_wrktravel_30d_end, ///
			filename("ATable14D_BootstrapIPW") title("Appendix Table 14D: Treatment Effects on Travel with Bootstrapping Inverse Probability Weighting") ///
			numboots(5000) /// 
			baselinevals winsorize /// 
			addlcontrols(`ipwcontrols') foldername("$rep_output")
			
		// ATABLE 14E (TRANSFERS)
		yemenreweightingtable gift_total_end totalremit_end, ///
			filename("ATable14E_BootstrapIPW") title("Appendix Table 14E: Treatment Effects on Inter-Household Transfers with Bootstrapping Inverse Probability Weighting") ///
			numboots(5000) /// 
			baselinevals winsorize /// 
			addlcontrols(`ipwcontrols') foldername("$rep_output")					
		*/
}

// ATABLE 15 (LIVESTOCK)
yemenstandardtable ls_hany_goatsheep_end ls_hany_sheep_end ls_hcnt_sheep_end ls_hany_goats_end ls_hcnt_goats_end ls_hany_mules_donkeys_end ls_hcnt_mules_donkeys_end ls_hany_chickens_end ls_hcnt_chickens_end ls_hany_doves_end ls_hcnt_doves_end ls_hany_cows_end ls_hcnt_cows_end ls_hany_rabbits_end ls_hcnt_rabbits_end, /// 
	filename("ATable15") title("Appendix Table 15: Treatment Effects on Count of Livestock") ///
	winsorize foldername("$rep_output")
	
sum ls_hany_goatsheep_bsl ls_hany_sheep_bsl ls_hcnt_sheep_bsl ls_hany_goats_bsl ls_hcnt_goats_bsl ls_hany_chickens_bsl ls_hcnt_chickens_bsl ls_hany_cows_bsl ls_hcnt_cows_bsl if treatment==0
 

